Page({
  data: {
    currentText: '',
    currentImage: '',
    currentIndex: 0,
    story: [
      { text: '太安八年，九月，惠宁公主与秋闱新晋状元柳陌雨大婚。哪曾想在大婚当天，在公主府内的众人还在推杯换盏之时，红帐房内的慧宁公主竟突然离奇死亡。你作为当今太子，皇上命你查明此事，抓住真凶，参破疑案。', storyimage: '../images/日常1.jpg', options: [] },
      { text: '经过一番调查，你将真凶锁定在了这二人当中： 公主侍女铃儿和驸马爷柳陌雨，两人都没有不在场证明。屋内有打斗的痕迹，你细心地发现了死者手中拽着一块绿色的纱布，似乎是从凶手身上拽下来的。而嫌疑犯之一的铃儿今天正好穿着绿色的纱裙！', storytwoimage: '../images/铃儿.jpg', storytwoimage: '../images/驸马笑.jpg',options: [] },
      { text: '你觉得调查已经有眉目了，前往大殿向太安帝禀报真凶。你觉得真凶是：', storyimage: '../images/微笑张嘴.jpg', options: [{ label: '铃儿', choice: '恭喜你找到真凶!原来铃儿的情郎被娇蛮的公主一不小心打死了，她伤心欲绝，偶得高人提点，在公主在大婚之日复仇。',storyimage: '../images/铃儿.jpg' }, { label: '驸马', choice: '冤枉无辜的驸马爷啦！读档重来吧！' ,storyimage: '../images/驸马怒.jpg'}] },
      { text: '体验结束！请前往官网购买游戏时间即可继续游戏！', storyimage: '../images/日常1.jpg', options: [] }
    ],
    options: [],
    showChapter: false,
    chapterTitle: ''
  },

  onLoad: function(options) {
    if (options.isNew === 'true') {
      this.startNewGame();
    } else {
      this.loadGame();
    }
  },

  startNewGame: function() {
    this.setData({
      currentIndex: 0,
      currentImage: '../images/日常1.jpg',
      options: [],
      showChapter: true,
      chapterTitle: '【第一章 惠宁之死】'
    });

    this.showStory();
  },

  loadGame: function() {
    const saveData = wx.getStorageSync('gameSave') || {};
    if (!Object.keys(saveData).length) {
      wx.showToast({
        title: '无存档可读取',
        icon: 'none'
      });
      return;
    }
    this.setData(saveData);
    this.updateStory();
  },

  updateStory: function() {
    const storyPart = this.data.story[this.data.currentIndex];
    this.setData({
      currentText: storyPart.text,
      currentImage: `path/to/images/A${this.data.currentIndex + 1}.png`, // 动态获取图片路径
      options: storyPart.options
    });
  },

  next: function() {
    if (this.data.showChapter) {
      this.setData({ showChapter: false, chapterTitle: '' });
      this.updateStory();
    } else if (this.data.currentIndex < this.data.story.length - 1) {
      this.setData({ currentIndex: this.data.currentIndex + 1 });
      this.updateStory();
    }
  },

  handleOptionSelect: function(e) {
    const choice = e.currentTarget.dataset.choice;
    console.log('用户选择了:', choice);

    // 根据选择更新当前文本和图像（这里需要根据实际情况调整）
    this.setData({
      currentText: choice,
      currentImage: `path/to/images/${this.data.currentIndex + 2}.png`, // 
      currentIndex: this.data.currentIndex + 1,
      options: []
    });

    this.saveGame();
  },

  saveGame: function() {
    const saveData = {
      currentIndex: this.data.currentIndex,
      currentText: this.data.currentText,
      currentImage: this.data.currentImage,
      options: this.data.options
    };
    wx.setStorage({
      key: 'gameSave',
      data: saveData
    });

    wx.showToast({
      icon: 'success'
    });
  },

  returnToMainPage: function() {
    // 返回封面页之前询问是否要保存当前进度
    wx.showModal({
      title: '提示',
      content: '离开前是否要存档？',
      success: (res) => {
        if (res.confirm) {
          this.saveGame();
        }
        wx.reLaunch({
          url: '/pages/cover/cover'
        });
      }
    });
  },

  showStory: function() {
    const that = this;
    wx.showToast({
      icon: 'none',
      duration: 3000,
      success: function() {
        setTimeout(function() {
          wx.hideToast();
          that.updateStory();
        }, 3000);
      }
    });
  }
});